package com.dhcc.wb.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;


public class HttpUtil {


	
	 public static void main(String args[]) {
		 
		 String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+WeiXin2ChangLiang.appid+"&secret="+WeiXin2ChangLiang.secret;
		 
	    
	     
	     String getarticlesummaryUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=27_RfyBJGiF_5XMhLI73_-V5lFik-qI6igSKcJoGphp5X6DdJuxige49fwW0PN3AL7jidGmS_RAmkyyD5q08c3A7q8XD6P6Ldr08hCWEwTb1fEBNZdxKAlcAq8DrfFz-oqjQE7hOcJ9oTnBMo_FUYReAFALPI";//获取图文群发每日数据

	     
	     JSONObject json = new JSONObject();
			json.put("begin_date", "2019-10-31");
			json.put("end_date", "2019-10-31");
	     System.out.println(postContent(getarticlesummaryUrl, json.toString()));
	     
	    }
	
	
	
	/**
	 * 获取页面源代码
	 * 主要处理get请求
	 */
	public static String  getContent(String urlstr) {
		StringBuffer line = new StringBuffer("");
	    try { //创建一个url对象来指向要采集信息的网址
	        URL url = new URL(urlstr); //将读取到的字节转化为字符
	        InputStream a = url.openStream();
	        InputStreamReader inStrRead = new InputStreamReader(a, "utf-8"); //读取InputStreamReader转化成的字符
	        BufferedReader bufRead = new BufferedReader(inStrRead); //读到的内容不为空
	        String getstr = "";
	        while ((getstr = bufRead.readLine()) != null) {
	        	line.append(getstr);
	        }
	        bufRead.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return line.toString();
	}
	
	/**
	 * 获取页面源代码
	 * 主要处理post请求
	 * params:"{\"begin_date\":\"2019-10-15\",\"end_date\":\"2019-10-16\"}"
	 */
	public static String  postContent(String urlstr,String params) {
		String result = "";
		try {
			  URL url = new URL(urlstr);// 创建连接  
	          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	          connection.setDoOutput(true);  
	          connection.setDoInput(true);  
	          connection.setUseCaches(false);  
	          connection.setInstanceFollowRedirects(true);  
	          connection.setRequestMethod("POST"); // 设置请求方式 
	          connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式  
	          connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式  
	          connection.connect();  
	          OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码  
//	          out.append("{\"begin_date\":\"2019-10-15\",\"end_date\":\"2019-10-16\"}");  
	          out.append(params);  
	          out.flush();  
	          out.close();  
	          // 读取响应  
	          int length = (int) connection.getContentLength();// 获取长度  
	          InputStream is = connection.getInputStream();  
	          if (length != -1) {  
	              byte[] data = new byte[length];  
	              byte[] temp = new byte[512];  
	              int readLen = 0;  
	              int destPos = 0;  
	              while ((readLen = is.read(temp)) > 0) {  
	                  System.arraycopy(temp, 0, data, destPos, readLen);  
	                  destPos += readLen;  
	              }  
	              result = new String(data, "UTF-8"); // utf-8编码  
	              if(result.startsWith("{\"errcode")) {
	            	  
	            	  JSONObject json = new JSONObject(result);
	            	  
//	            	  System.out.println(CodeMap.getCode().get(String.valueOf(json.getInt("errcode"))));
	              }
	          }  
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
	}
	
	
	
}
